package handler

import (
	"course/api/request"
	"course/interal/dao"
	"course/interal/model"
	"course/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
)

func CreateVideo(c *gin.Context) {
	var req request.CreateVideo
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var video model.Videos
	dao.GetOneByFields(&model.Videos{}, &video)
	if video.Name == req.Name {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "此视频已存在",
			"data": nil,
		})
		return
	}
	video = model.Videos{
		Name:                  req.Name,
		VideoDescription:      req.VideoDescription,
		FounderId:             req.FounderId,
		AuthorId:              req.AuthorId,
		ReviewerId:            req.ReviewerId,
		Keywords:              req.Keywords,
		FirstProfessional:     req.FirstProfessional,
		SecondaryProfessional: req.SecondaryProfessional,
		AuditStatus:           req.AuditStatus,
	}
	censor := pkg.Censor(req.Name)

	if censor {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "内容违规，请重新输入",
			"data": nil,
		})
		return
	}

	if !dao.Create(&video) {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "视频初始化数据失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": video,
	})
}

func ListVideo(c *gin.Context) {
	var video []model.Videos
	var req request.FindVideo
	dao.GetListByFields(&model.Videos{
		Name:                  req.Name,
		VideoDescription:      req.VideoDescription,
		FounderId:             req.FounderId,
		AuthorId:              req.AuthorId,
		ReviewerId:            req.ReviewerId,
		Keywords:              req.Keywords,
		FirstProfessional:     req.FirstProfessional,
		SecondaryProfessional: req.SecondaryProfessional,
		AuditStatus:           req.AuditStatus,
	}, &video)
	var videos []*model.Videos
	for _, v := range video {
		videos = append(videos, &v)
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": videos,
	})
}

func UpdateVideo(c *gin.Context) {
	var req request.UpdateVideo
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	if req.ReviewerId == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "审核人为空,状态默认未启用",
			"data": dao.Update(&model.Videos{EnableStatus: 2}),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": dao.GetOneById(uint(req.Id), &model.Videos{}),
	})
}

func DelVideo(c *gin.Context) {
	var req request.DelVideo
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": dao.Delete(&model.Videos{Id: req.Id}),
	})
}

func InfoVideo(c *gin.Context) {
	var req request.InfoVideo
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var video []model.Videos
	dao.GetListByFields(&model.Videos{
		Id: req.Id,
	}, &video)
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": video,
	})
}

func CreateComment(c *gin.Context) {
	var req request.CreateComment
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var com model.Comments
	dao.GetOneByFields(&model.Comments{}, &com)
	com = model.Comments{
		Num:     int64(req.Num),
		Content: req.Content,
	}
	if !dao.Create(&com) {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "弹幕发布失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": http.StatusOK,
		"msg":  "ok",
		"data": com,
	})
}
